package no.ntnu.fp.su;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import no.ntnu.fp.db.Database;

/**
 * Events on the platform
 * 
 * @author abs
 * 
 */
public class Events {

	private ArrayList<Event> events;
	private int platformid;
	private Database db;
	private boolean read;

	/**
	 * 
	 * @param {@link Integer} ID of the {@link Platform} these {@link Event}s
	 *        belong
	 * @param {@link Database} The Database connected to the platform
	 */
	public Events(int platformid, Database db) {
		this.platformid = platformid;
		this.db = db;
		read = false;
		events = new ArrayList<Event>();
	}

	/**
	 * Get the {@link Event}s in this eventlist
	 * 
	 * @return {@link ArrayList<Event>}
	 */
	public ArrayList<Event> getEvents() {
		return events;
	}

	/**
	 * 
	 * @return {@link Integer} <br>
	 *         number of events
	 * 
	 */
	public int getEventSize() {
		return events.size();
	}

	/**
	 * save a new event to the DB and add it
	 * 
	 * @param {@link Event} the event to add
	 */
	public void addEvent(Event ev) {
		db.connect();
		events.add(ev);
		db.execute("insert into events (platformid, time, content) values("
				+ platformid + ",\"" + ev.getTime().toString() + "\",\""
				+ ev.getContent() + "\")");
		db.disconnect();
	}

	/**
	 * Read the {@link event}s from the {@link Database}
	 */
	public void getEventsFromDB() {
		if (read) {
			System.out.println("events already read");
			return;
		}
		db.connect();

		ResultSet rs;
		if (platformid != 0) {
			rs = db.query("select * from events where time > \""
					+ (new Timestamp(System.currentTimeMillis()
							- (24 * 60 * 60 * 1000)).toString()) + "\""
					+ " and platformid = " + platformid);
		} else {
			rs = db.query("select * from events where time > \""
					+ (new Timestamp(System.currentTimeMillis()
							- (24 * 60 * 60 * 1000)).toString()) + "\"");
		}

		Event ev;
		try {
			while (rs.next()) {
				ev = new Event(rs.getInt(1), rs.getTimestamp(3), rs
						.getString(4));
				events.add(ev);

			}
			read = true;
		} catch (SQLException e) {
			System.out.println("error when importing events from db");
			e.printStackTrace();
		}
		db.disconnect();
	}

	/**
	 * Delete an event locally and in the DB
	 * 
	 * @param {@link Integer} index of the event
	 */
	public void deleteEvent(int index) {
		db.connect();
		db.execute("delete from table events where eventid=" + index);
		System.out.println("deleted event " + index);
		db.disconnect();

		events.remove(getEventID(index));
	}

	/**
	 * get the internal id in the eventarray belonging to the eventid
	 * 
	 * @param {@link Integer} event index
	 * @return {@link Integer} the internal index
	 */
	private int getEventID(int index) {
		for (Event ev : events) {
			if (ev.getId() == index) {
				return events.indexOf(ev);
			}
		}
		return 0;
	}

	/**
	 * Get free ID for new {@link Event}
	 * 
	 * @return first available index
	 */
	public synchronized int newId() {
		if (!read)
			throw new NullPointerException("events need to be read first");
		boolean free = false;
		for (int i = 0; i < 1000; i++) {
			free = true;
			for (Event ev : events) {
				if (ev.getId() == i) {
					free = false;
					break;
				}
			}
			if (free)
				return i;
		}
		return 0;
	}

}
